package com.yunxin.customui.service.executesqlimpl;

import com.yunxin.customui.util.PageUtil;
import com.yunxin.customui.bean.card.CardConfig;
import com.yunxin.customui.myinterface.IEventHandler;
import com.yunxin.customui.mapper.ExecuteMapper;
import com.yunxin.customui.myinterface.IExecuteSql;
import com.yunxin.customui.util.CustomSQLUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 对数据进行Write操作的策略，返回影响行数
 *
 * @author lee
 * @date 2021/10/05
 */
@Service
public class ExecuteWriteSql implements IExecuteSql<Integer> , IEventHandler<CardConfig,Map<String, Object>> {

    @Autowired
    ExecuteMapper executeMapper;

    /**
     * 执行
     *
     * @param params   数据库配置的参数
     * @param paramMap 外部传递的参数
     * @return  影响行数
     */
    @Override
    public Integer execute(String[] params, Map<String, Object> paramMap) {
        String sql = CustomSQLUtil.buildSQL(params[0], paramMap);
        return executeMapper.executeWriteSql(sql);
    }

    @Override
    public List<EventHandlerResult> eventHandle(CardConfig config, Map<String, Object> paramMap) {
        List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(config.getEventHandler());
        Integer execute = this.execute(methodParams.get(0).getParams(), paramMap);
        return Collections.singletonList(new EventHandlerResult(ResultEventType.Tip,execute));
    }
}
